#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

#include "parser.h"

int openDonj (char *path)
{
	int	fd;
	fd = open (path, O_RDONLY);
	return fd;
}


void getLine (int fd, char *buffer)
{
	char tmp = '\0';
	int i = 0;

	while (0 < read (fd, &tmp, 1))
	{
		buffer[i] = tmp;
                i++;
		if (tmp == '\n')
		{
			break;
		}
	}
	buffer[i]='\0';
}

void get_ints (char *str, size_t s, int **datat)
{
	const char 		sep = ':', space = ' ';
	size_t			size, i, j, k, l;
	char			*tmp, *nb;
	int			    *datas;
	datas = *datat;
	size = strlen(str) - 1;


	for (i = 0; sep != str[i]; i++);
	i += 2;
	tmp = malloc (size - i);
	j = i;
	for (; i < size; i++) tmp [i - j] = str[i];
	i = 0;
	for (j = 0; j < s; j++)
	{
		datas [j] = -1;
		k = i;
		l = i;
		for (; i < size && space != tmp[i]; i++);
		nb = malloc (i);
		for (; k < i; k++) nb[k-l] = tmp[k];
		datas[j] = atoi (nb);
		free (nb);
		i++;
	}
	free (tmp);
}

int get_charact (char *ph, int *s1, float *c, int *s2)
{

	int			    *datas;
	float			cout;
	datas = malloc (sizeof(int) * 3);

	if (ph[0] == 'R')
	{
		get_ints (ph, 1, &datas);
		*s1 = datas[0];
		*s2 = datas[2];
		*c = (cout = datas [1]);
		return 1;
	}
	else if (ph[0] == 'M')
	{
		get_ints (ph, 2, &datas);
		*s1 = datas[0];
        *s2 = datas[2];
        *c = (cout = datas [1]);
		return 2;
	}
	else if (ph[0] == 'B')
    {
		get_ints (ph, 2, &datas);
		*s1 = datas[0];
        *s2 = datas[2];
        *c = (cout = datas [1]);
		return 3;
    }
	else if (ph[0] == 'D')
    {
		get_ints (ph, 1, &datas);
	    *s1 = datas[0];
        *s2 = datas[2];
        *c = (cout = datas [1]);
		return 4;
    }
	else if (ph[0] == 'A')
    {
		if (ph[3] == 'S')
		    return 5;
		get_ints (ph, 3, &datas);
		*s1 = datas[0];
        *s2 = datas[2];
        *c = (cout = datas [1]);
		return 6;
    }
	else
	{
		*s1 = *s1;
        *s2 = *s2;
        *c = *c;
		return -1;
	}
}

t_graph createDonjon (char *path)
{
	int	        fd, ctl;
	int	        s1,s2;
	float 	    c;
	t_graph     donjon = NULL;
    char ligne [20];
	fd = openDonj (path);
	do
	{
		getLine (fd, ligne);
		ctl = get_charact (ligne, &s1, &c, &s2);
		switch (ctl)
		{
            case 1 : /* nouveau donjon */
			    donjon = create_graph(s1+1);
                break;
            case 2 : /* Monster */
                add_somCost(donjon, s1, c);
                break;
            case 3 : /* Brevery */
                c = c * (-1);
                add_somCost(donjon, s1, c);
                break;
            case 4 : /* Death */
                add_somCost(donjon, s1, +INFINITY);
                break;
            case 5 : /* debut arcs */
                break;
			case 6 : /* nouvel arc */
				add_arc(donjon, s1, s2, c);
				break;
        }
	}while (ligne[0] != '\0');
    close (fd);
	return donjon;
}

